package com.ruoyi.web.controller.workflow;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workflow.domain.BpmnInfo;
import com.ruoyi.workflow.service.IBpmnInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程定义信息Controller
 *
 * @author cxh
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/workflow/bpmninfo")
public class BpmnInfoController extends BaseController {
    @Autowired
    private IBpmnInfoService bpmnInfoService;

    /**
     * 查询流程定义信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:bpmninfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BpmnInfo bpmnInfo) {
        startPage();
        List<BpmnInfo> list = bpmnInfoService.selectBpmnInfoList(bpmnInfo);
        return getDataTable(list);
    }

    /**
     *
     */
    @PreAuthorize("@ss.hasPermi('business:bpmninfo:view')")
    @GetMapping("/resources/{id}")
    public void viewResource(@PathVariable Long id, String type, HttpServletResponse resp) throws IOException {
        InputStream resource = bpmnInfoService.getResourceByType(id,type);
        //将资源输出流拷贝到响应输出流
        IOUtils.copy(resource,resp.getOutputStream());
    }


    /**
     * 删除流程定义信息
     */
    @PreAuthorize("@ss.hasPermi('business:bpmninfo:remove')")
    @Log(title = "删除流程定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bpmnInfoService.deleteDefinition(ids));
    }

    /**
     * 支持的后缀
     */
    private static  final List<String> SUPPORT_SUFFIXES = Arrays.asList("bpmn","xml","zip");

    @PreAuthorize("@ss.hasPermi('business:bpmninfo:deploy')")
    @Log(title = "流程定义部署", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public AjaxResult deploy(MultipartFile file ,BpmnInfo bpmnInfo) throws IOException {
        //文件类型校验
        String filename = file.getOriginalFilename();//原始文件名
        String suffix = FileUtils.getSuffix(filename);//文件后缀名
        if (!SUPPORT_SUFFIXES.contains(suffix)){
            return AjaxResult.error(HttpStatus.BAD_REQUEST,"目前不支持提交"+SUPPORT_SUFFIXES+"类型的流程文件");
        }
        bpmnInfo.setResourceName(filename);
        //部署
        bpmnInfoService.deploy(bpmnInfo,file.getInputStream());
        return AjaxResult.success("文件部署成功");
    }
}
